import {
    ElLoading,
    ElMessage,
    ElMessageBox,
    ElNotification,
    type ElMessageBoxOptions,
    type LoadingOptions,
    type MessageParams,
    type NotificationParams
} from 'element-plus'
import type { AppContext } from 'vue'

/**
 * 封装 ElMessage 方法，确保在下一个 message 弹出之前关闭其他的 message
 */
const showMessage = (options?: MessageParams, appContext?: null | AppContext) => {
  ElMessage.closeAll()
  return ElMessage(options, appContext)
}

// 封装常用的消息类型方法
export const $message = {
  success: (
    message: string,
    options?: Omit<Parameters<typeof ElMessage>[0], 'message' | 'type'>
  ) => {
    return showMessage({ type: 'success', message, ...options })
  },
  warning: (
    message: string,
    options?: Omit<Parameters<typeof ElMessage>[0], 'message' | 'type'>
  ) => {
    return showMessage({ type: 'warning', message, ...options })
  },
  info: (message: string, options?: Omit<Parameters<typeof ElMessage>[0], 'message' | 'type'>) => {
    return showMessage({ type: 'info', message, ...options })
  },
  error: (message: string, options?: Omit<Parameters<typeof ElMessage>[0], 'message' | 'type'>) => {
    return showMessage({ type: 'error', message, ...options })
  },
  closeAll: ElMessage.closeAll
}

/**
 * 封装 ElLoading 方法
 */
const showLoading = (options?: LoadingOptions | string) => {
  if (typeof options === 'string') {
    options = {
      text: options,
      lock: true,
      background: 'rgba(0, 0, 0, 0.7)'
    }
  }
  return ElLoading.service(options)
}

export const $loading = {
  show: showLoading,
  close: (instance: ReturnType<typeof ElLoading.service>) => {
    instance.close()
  }
}

/**
 * 封装 ElNotification 方法
 */
const showNotification = (options?: NotificationParams, appContext?: null | AppContext) => {
  return ElNotification(options, appContext)
}

export const $notification = {
  success: (
    message: string,
    title?: string,
    options?: Omit<Parameters<typeof ElNotification>[0], 'message' | 'type' | 'title'>
  ) => {
    return showNotification({ type: 'success', message, title, ...options })
  },
  warning: (
    message: string,
    title?: string,
    options?: Omit<Parameters<typeof ElNotification>[0], 'message' | 'type' | 'title'>
  ) => {
    return showNotification({ type: 'warning', message, title, ...options })
  },
  info: (
    message: string,
    title?: string,
    options?: Omit<Parameters<typeof ElNotification>[0], 'message' | 'type' | 'title'>
  ) => {
    return showNotification({ type: 'info', message, title, ...options })
  },
  error: (
    message: string,
    title?: string,
    options?: Omit<Parameters<typeof ElNotification>[0], 'message' | 'type' | 'title'>
  ) => {
    return showNotification({ type: 'error', message, title, ...options })
  },
  closeAll: ElNotification.closeAll
}

/**
 * 封装 ElMessageBox 确认框
 */
export const $confirm = (
  message: string,
  title: string = '提示',
  options?: ElMessageBoxOptions
) => {
  return ElMessageBox.confirm(message, title, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    ...options
  })
}

